<?php


use Phinx\Migration\AbstractMigration;

class FillDefaultAttributeData extends AbstractMigration
{

    protected $attributeTable;

    protected $attributeValueTable;

    protected $stepCategoryAttributeTable;

    /**
     * 获取所有工序分类数据
     * @return array
     */
    public function getAllStepCategoryData()
    {
        $sql = 'select * from step_category';
        $result = $this->query($sql);
        $info = array();
        foreach ($result as $key => $val) {
            $info[$val['code']] = $val;
        }
        return $info;
    }


    /**
     * 递归添加属性和属性值
     * @param $attributeData
     * @param $allStepCategoryData
     * @throws Exception
     */
    public function recursionAttribute($attributeData, $allStepCategoryData)
    {
        foreach ($attributeData as $attributeItem) {
            $attributeAdd = $attributeItem['data'];
            $attributeAdd['uuid'] = Webpatser\Uuid\Uuid::generate()->string;
            $this->attributeTable->insert($attributeAdd)->save();
            $query = $this->fetchRow('SELECT max(`id`) as id FROM attribute');

            // 工序分类关联属性
            foreach ($attributeItem['module_code'] as $moduleCode) {
                $stepCategoryAttributeItem = [
                    'attribute_id' => $query['id'],
                    'step_category_id' => $allStepCategoryData[$moduleCode]['id']
                ];

                $this->stepCategoryAttributeTable->insert($stepCategoryAttributeItem)->save();
            }

            foreach ($attributeItem['values'] as $value => $variable) {
                $attributeValueAdd = [
                    'value' => $value,
                    'attribute_id' => $query['id'],
                    'variable' => $variable,
                    'uuid' => Webpatser\Uuid\Uuid::generate()->string
                ];
                $this->attributeValueTable->insert($attributeValueAdd)->save();
            }
        }
    }

    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex13
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */

    /**
     * Migrate Up.
     * @throws Exception
     */
    public function up()
    {
        $attributeData = [
            [
                'module_code' => ['asset', 'sequence', 'shot', 'level'],
                'data' => [
                    'name' => '风格',
                    'code' => 'fengge'
                ],
                'values' => [
                    '写实' => 1,
                    '卡通' => 0.8,
                    '三渲二' => 0.6
                ]
            ],
            [
                'module_code' => ['asset'],
                'data' => [
                    'name' => '资产类型',
                    'code' => 'zichanleixing'
                ],
                'values' => [
                    '角色' => 1,
                    '道具' => 1,
                    '场景' => 1,
                    '关卡' => 1
                ]
            ],
            [
                'module_code' => ['asset', 'level'],
                'data' => [
                    'name' => '等级',
                    'code' => 'dengji'
                ],
                'values' => [
                    'S' => 1,
                    'A' => 0.8,
                    'B' => 0.6,
                    'C' => 0.4
                ]
            ],
            [
                'module_code' => ['sequence', 'shot'],
                'data' => [
                    'name' => '镜头类型',
                    'code' => 'jingtouleixing'
                ],
                'values' => [
                    '超级特写' => 1,
                    '极至特写' => 1,
                    '中等特写' => 1.05,
                    '详细特写' => 1.1,
                    '广角特写' => 1.15,
                    '近景' => 1.2,
                    '中近景' => 1.15,
                    '中景' => 1,
                    '牛仔拍摄' => 1.05,
                    '中全景' => 1,
                    '全景' => 1
                ]
            ],
            [
                'module_code' => ['sequence', 'shot'],
                'data' => [
                    'name' => '表演种族',
                    'code' => 'biaoyanzhongzu'
                ],
                'values' => [
                    '人' => 1,
                    '机甲' => 1,
                    '怪兽' => 1.2
                ]
            ],
            [
                'module_code' => ['sequence', 'shot'],
                'data' => [
                    'name' => '表演人数',
                    'code' => 'biaoyanrenshu'
                ],
                'values' => [
                    '1' => 1,
                    '2' => 1.1,
                    '3' => 2,
                    '4' => 2.5,
                    '5' => 3,
                    '5~' => 3.5
                ]
            ],
            [
                'module_code' => ['sequence', 'shot'],
                'data' => [
                    'name' => '表演形式',
                    'code' => 'biaoyanxingshi'
                ],
                'values' => [
                    '文戏' => 1,
                    '武戏' => 2
                ]
            ],
            [
                'module_code' => ['sequence', 'shot'],
                'data' => [
                    'name' => '镜头时长',
                    'code' => 'jingtoushichang'
                ],
                'values' => [
                    '~3' => 1,
                    '~6' => 1.05,
                    '~9' => 3,
                    '~12' => 4,
                    '~15' => 5
                ]
            ],
            [
                'module_code' => ['sequence', 'shot'],
                'data' => [
                    'name' => '制作方式',
                    'code' => 'zhizuofangshi'
                ],
                'values' => [
                    '捕捉' => 1,
                    '手K' => 2
                ]
            ]
        ];


        $allStepCategoryData = $this->getAllStepCategoryData();
        $this->attributeTable = $this->table('attribute');
        $this->attributeValueTable = $this->table('attribute_value');
        $this->stepCategoryAttributeTable = $this->table('step_category_attribute');

        $this->recursionAttribute($attributeData, $allStepCategoryData);
    }

    /**
     * Migrate Down.
     */
    function down()
    {
        $this->execute('DELETE FROM attribute');
        $this->execute('DELETE FROM attribute_value');
        $this->execute('DELETE FROM step_category_attribute');
    }
}
